package com.maxlen.camunda.utils;


import camundafeel.de.odysseus.el.ExpressionFactoryImpl;
import camundafeel.de.odysseus.el.util.SimpleContext;
import camundafeel.javax.el.ExpressionFactory;
import camundafeel.javax.el.ValueExpression;
import com.maxlen.camunda.constant.CamundaConstant;
import org.camunda.bpm.engine.delegate.Expression;
import org.camunda.bpm.engine.impl.RepositoryServiceImpl;
import org.camunda.bpm.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
import org.camunda.bpm.engine.task.Task;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CamundaUtils {

    /**
     * 将 Object 类型转为 List
     * @param variable 参数
     * @return 结果
     */
    public static List<String> ObjectChangeArrays(Object variable){
        List<String> assigneeList = new ArrayList<>();
        if(variable instanceof ArrayList<?>){
            for (Object o : (List<?>) variable) {
                assigneeList.add(o.toString());
            }
        }
        return assigneeList;
    }

    /**
     * 解析el表达式
     * @param variable 参数
     * @param el 表达式
     * @return 结果
     */
    public static Boolean isCondition(Map<String, Object> variable, String el){
        try {
            ExpressionFactory factory = new ExpressionFactoryImpl();
            SimpleContext context = new SimpleContext();
            if (variable != null) {
                for (Map.Entry<String, Object> value : variable.entrySet()) {
                    context.setVariable(value.getKey(), factory.createValueExpression(value.getValue(), String.class));
                }
            }
            ValueExpression e = factory.createValueExpression(context, el, boolean.class);
            return (Boolean) e.getValue(context);
        } catch (Exception e) {
            return false;
        }
    }

    /** 获取节点的el表达式 */
//    public static void getNodeEl(){
//        Task task = processEngine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
//
//        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) processEngine.getRepositoryService())
//                .getDeployedProcessDefinition(task.getProcessDefinitionId());
//        String activityId = task.getTaskDefinitionKey();
//        List<ActivityImpl> activities = processDefinitionEntity.getActivities();
//
//        ActivityImpl num = null;
//        for (ActivityImpl activity : activities) {
//            if(activity.getId().replace(CamundaConstant.MULTI_INSTANCE_BODY, "").equals(activityId)){
//                MultiInstanceActivityBehavior activityBehavior = (MultiInstanceActivityBehavior) activity.getActivityBehavior();
//                Expression expression = activityBehavior.getCompletionConditionExpression();
//                Map<String, Object> variables = processEngine.getRuntimeService().getVariables(task.getExecutionId());
//                Integer nrOfInstances = (Integer) variables.get(CamundaConstant.NR_OF_COMPLETED_INSTANCES);
//                variables.put(CamundaConstant.NR_OF_COMPLETED_INSTANCES, nrOfInstances + 1);
//                Boolean condition = CamundaUtils.isCondition(variables, expression.getExpressionText());
//                System.out.println();
//            }
//        }
//    }

}
